package routes

import (
	"blog_server/controller"
	"blog_server/middleware"
	"blog_server/utils"
	"github.com/gin-gonic/gin"
	"net/http"
)

func InitRouter() {
	gin.SetMode(utils.AppMode)
	r := gin.Default()

	r.StaticFS("/images", http.Dir("static/images"))

	r.Use(middleware.CORSMiddleware())

	r.POST("/register", controller.Register)
	r.POST("/login", controller.Login)

	r.POST("/upload", controller.Upload)
	r.POST("/upload/rich_editor_upload", controller.RichEditorUpload)

	r.GET("/category", controller.GetCategoryList)
	r.GET("/category/:id", controller.GetCategory)

	verifyRoute := r.Group("/verification")
	{
		verifyRoute.POST("/send", controller.SendCode)
		verifyRoute.POST("/verify", controller.VerifyCode)
	}

	userRoutes := r.Group("/user")
	{
		userRoutes.GET("", middleware.AuthMiddleware(), controller.GetInfo)
		userRoutes.GET("briefInfo/:id", controller.GetBriefInfo)
		userRoutes.GET("phone/:phone_number", controller.GetInfoByPhoneNumber)
		userRoutes.GET("detailedInfo/:id", middleware.AuthMiddleware(), controller.GetDetailedInfo)
		userRoutes.PUT("avatar/:id", middleware.AuthMiddleware(), controller.UpdateAvatar)
		userRoutes.PUT("name/:id", middleware.AuthMiddleware(), controller.UpdateName)
		userRoutes.PUT("password", controller.UpdatePassword)
	}

	colRoutes := r.Group("/collects")
	{
		colRoutes.Use(middleware.AuthMiddleware())
		colRoutes.GET(":id", controller.Collected)
		colRoutes.PUT("new/:id", controller.NewCollect)
		colRoutes.DELETE(":id", controller.UnCollect)
	}

	folRoutes := r.Group("/following")
	{
		folRoutes.Use(middleware.AuthMiddleware())
		folRoutes.GET(":id", controller.Followed)
		folRoutes.PUT("new/:id", controller.Following)
		folRoutes.DELETE(":id", controller.UnFollowing)
	}
	//文章接口
	articleRoutes := r.Group("/article")
	{
		articleRoutes.POST("", middleware.AuthMiddleware(), controller.CreateArt)      //发布文章
		articleRoutes.PUT(":id", middleware.AuthMiddleware(), controller.UpdateArt)    // 修改文章
		articleRoutes.DELETE(":id", middleware.AuthMiddleware(), controller.DeleteArt) //删除文章
		articleRoutes.GET(":id", controller.ShowArt)                                   // 查询文章
		articleRoutes.POST("list", controller.ShowArtList)                             //显示文章列表
	}

	commentRoutes := r.Group("/comment")
	{
		commentRoutes.GET("list/:id", controller.ShowCommentList)
		commentRoutes.PUT("add/:id", middleware.AuthMiddleware(), controller.AddComment)
		commentRoutes.GET("show/:id", middleware.AuthMiddleware(), controller.ShowComment)
		commentRoutes.GET("count/:id", controller.ShowCommentsCount)
	}

	replyRoutes := r.Group("/reply")
	{
		replyRoutes.PUT("add/:id", middleware.AuthMiddleware(), controller.AddReply)
		replyRoutes.GET("list/:id", middleware.AuthMiddleware(), controller.ShowReplyList)
	}

	_ = r.Run(":8080")
}
